/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.ka.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ka.entity.KaSize;
import com.kaaction.modules.ka.service.KaSizeService;

/**
 * 尺码Controller
 * @author huanghaiming
 * @version 2017-06-14
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/kaSize")
public class KaSizeController extends BaseController {

	@Autowired
	private KaSizeService kaSizeService;
	
	@ModelAttribute
	public KaSize get(@RequestParam(required=false) String id) {
		KaSize entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kaSizeService.get(id);
		}
		if (entity == null){
			entity = new KaSize();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:kaSize:view")
	@RequestMapping(value = {"list", ""})
	public String list(KaSize kaSize, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KaSize> page = kaSizeService.findPage(new Page<KaSize>(request, response), kaSize); 
		model.addAttribute("page", page);
		return "modules/ka/kaSizeList";
	}

	@RequiresPermissions("ka:kaSize:view")
	@RequestMapping(value = "form")
	public String form(KaSize kaSize, Model model) {
		model.addAttribute("kaSize", kaSize);
		return "modules/ka/kaSizeForm";
	}

	@RequiresPermissions("ka:kaSize:edit")
	@RequestMapping(value = "save")
	public String save(KaSize kaSize, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kaSize)){
			return form(kaSize, model);
		}
		
		int cnt = kaSizeService.getEqual(kaSize);
		if(cnt>0){
			model.addAttribute("message", "该尺码已存在");
			return form(kaSize, model);
		}
		kaSizeService.save(kaSize);
		addMessage(redirectAttributes, "保存尺码成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaSize/?repage";
	}
	
	@RequiresPermissions("ka:kaSize:delete")
	@RequestMapping(value = "delete")
	public String delete(KaSize kaSize, RedirectAttributes redirectAttributes) {
		kaSizeService.delete(kaSize);
		addMessage(redirectAttributes, "删除尺码成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaSize/?repage";
	}

}